//
//  LeftViewController.m
//  LimitFree
//
//  Created by Hailong.wang on 15/8/26.
//  Copyright (c) 2015年 Hailong.wang. All rights reserved.
//

#import "LeftViewController.h"

#import "SubClassifyModel.h"
#import "CategoryTableViewCell.h"

static LeftViewController *lvc = nil;

@interface LeftViewController () <UITableViewDataSource, UITableViewDelegate> {
    //数据源
    JSONModelArray*_data;
    //表视图
    UITableView *_tableView;
    //黑色的透明背景
    UIView *_backView;
    //声明block变量
    GetCategory _selectBlock;
    
   
}

@end

@implementation LeftViewController

+ (LeftViewController *)shareSingleton {
    @synchronized (self) {
        if (!lvc) {
            lvc = [[LeftViewController alloc] init];
        }
    }
    return lvc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
}

- (void)initData {
   __block NSArray* temp=nil;
    [NetRequestManager GET:kCateUrl parame:nil succes:^(AFHTTPRequestOperation *operation, id responseObject) {
        temp=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        //JSONModel接收数据NSData
        _data=[[JSONModelArray alloc]initWithArray:temp modelClass:[SubClassifyModel class]];
        NSLog(@"%@",_data);
        [self refreshTableView];
    } failed:^(AFHTTPRequestOperation *operation, NSError *error) {
        //获取网络数据失败
        NSLog(@"%@", error);
    }];
}

//刷新列表
- (void)refreshTableView {
    [_tableView reloadData];
}

- (void)createView {
    _backView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64)];
    _backView.backgroundColor = RGBA(0, 0, 0, 0.5f);
    [self.view addSubview:_backView];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //先不添加到self.view上
}

//显示
- (void)showView:(GetCategory)category {
    //接受block数据
    _selectBlock=[category copy];
    
    
    //取出画板
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    //把当前控制器的view贴在画板上
    [window addSubview:self.view];
    //将tableView准备好，并且贴在屏幕外边
    [_tableView setFrame:CGRectMake(0 - ScreenWidth, 64, ScreenWidth - LeftDistance * 5, ScreenHeight - 64)];
    [self.view addSubview:_tableView];
    //移动入屏幕
    [UIView animateWithDuration:0.5 animations:^{
        [_tableView setXOffset:0];
    }];
    

}

//隐藏
- (void)hideView {
    //动画还原tableView移出屏幕
    [UIView animateWithDuration:0.5 animations:^{
        [_tableView setXOffset:0 - ScreenWidth];
    } completion:^(BOOL finished) {
        //动画结束后移除tableView和self.view
        [_tableView removeFromSuperview];
        [UIView animateWithDuration:0.1 animations:^{
            _backView.backgroundColor = RGBA(0, 0, 0, 0);
        } completion:^(BOOL finished) {
            [self.view removeFromSuperview];
            _backView.backgroundColor = RGBA(0, 0, 0, 0.5);
        }];
    }];
}

//隐藏
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self hideView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"CategoryIdentifier";
    CategoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    /*if (!cell) {
        cell = [[CategoryTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }*/
    
    if (!cell) {
        cell=[[[NSBundle mainBundle]loadNibNamed:@"CategoryTableViewCell" owner:self options:nil] lastObject];
    }
    //取得数据元
    SubClassifyModel *model = [_data objectAtIndex:indexPath.row];
    //设置分类名称
    cell.categoryName.text = model.categoryCname;
    //下载图片
    [cell.categoryImage sd_setImageWithURL:[NSURL URLWithString:model.picUrl]];
    //加圆角
    cell.categoryImage.layer.cornerRadius=10.f;
    cell.categoryImage.layer.masksToBounds=YES;
    //填写内容
    cell.content.text = [NSString stringWithFormat:@"应用总数%@，其中限免的有%@", model.categoryCount, model.limited];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80.f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //取得数据源
    SubClassifyModel* model=[_data objectAtIndex:indexPath.row];
    //调用block的方法，把参数传回去
    _selectBlock(model.categoryId);
    
    //点击收回 666
    [self hideView];
}
@end





